-
Notifications
You must be signed in to change notification settings - Fork 0
/
zad3.py
46 lines (33 loc) · 880 Bytes
/
zad3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
""" O(n^2), gdzie n - liczba różnych zadań (bo trzeba przejrzeć macierz T """
from zad3testy import runtests
def create_graph(T):
n = len(T)
G = [[] for _ in range(n)]
for a in range(n):
for b in range(n):
if T[a][b] == 1:
G[a].append(b)
elif T[a][b] == 2:
G[b].append(a)
return G
def topological_sort(G: 'graph represented using adjacency lists'):
n = len(G)
visited = [False] * n
result = [None] * n
idx = n
def dfs(u):
visited[u] = True
for v in G[u]:
if not visited[v]:
dfs(v)
nonlocal idx
idx -= 1
result[idx] = u
for u in range(n):
if not visited[u]:
dfs(u)
return result
def tasks(T):
G = create_graph(T)
return topological_sort(G)
runtests(tasks)